﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Objects;

namespace Library
{
    public partial class BookBook : Form
    {
        LibraryEntities1 ins = new LibraryEntities1();
        public static ObjectSet<Book> books;

        public BookBook()
        {
            InitializeComponent();
            books = ins.Book;
        }

        private void добавитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BookViewForm fm = new BookViewForm(null, true);
            fm.ShowDialog();
            books = ins.Book;
            BookForm_Load(sender, e);
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bindingSource1_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void BookForm_Load(object sender, EventArgs e)
        {
            bookView.Columns.Add("bookId", "bookId");
            bookView.Columns["bookId"].Visible = false;
            
            bookView.Columns.Add("ISBN", "ISBN");
            bookView.Columns["ISBN"].ReadOnly = true;

            bookView.Columns.Add("Name", "Название");
            bookView.Columns["Name"].Width = 200;
            bookView.Columns["Name"].ReadOnly = true;

            bookView.Columns.Add("Location", "Место");
            bookView.Columns["Location"].Width = 200;
            bookView.Columns["Location"].ReadOnly = true;


            int i = 0;
            bookView.Rows.Clear();
            foreach (Book book in books)
            {
                bookView.Rows.Add();
                bookView.Rows[i].Cells[0].Value = book.bookid;
                bookView.Rows[i].Cells[1].Value = book.ISBN;
                bookView.Rows[i].Cells[2].Value = book.BookName;
                bookView.Rows[i].Cells[3].Value = book.Location;
                i++;
            }




            //string connectionString = ConfigurationManager.ConnectionStrings["Library.Properties.Settings.LibraryConnectionString"].ConnectionString;
            //SqlConnection conn = new SqlConnection();
            //conn.ConnectionString = connectionString;
            //SqlCommand myCommand = conn.CreateCommand();
            //myCommand.CommandType = CommandType.StoredProcedure;
            //myCommand.CommandText = "[getAllBooks]";
            //conn.Open();
            //SqlDataAdapter dataAdapter = new SqlDataAdapter();
            //dataAdapter.SelectCommand = myCommand;
            //DataSet ds = new DataSet();
            //dataAdapter.Fill(ds, "Books");
            //conn.Close();
            //bookView.DataSource = ds.Tables["Books"].DefaultView;
            //bookView.Columns[1].Width = 250;
        }

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            textBox1.Text = String.Empty;
        }

        private void редактироватьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ins = new LibraryEntities1();
            books = ins.Book;
           
                if (bookView.CurrentRow != null)
                {

                    int bookId = Convert.ToInt32(bookView.CurrentRow.Cells[0].Value);
                    Book book = books.Where(x => x.bookid == bookId).FirstOrDefault();
                    BookViewForm fm = new BookViewForm(book, false);
                    fm.ShowDialog();
                    bookView.CurrentRow.Cells[0].Value = fm.book.bookid;
                    bookView.CurrentRow.Cells[1].Value = fm.book.ISBN;
                    bookView.CurrentRow.Cells[2].Value = fm.book.BookName;
                    bookView.CurrentRow.Cells[3].Value = fm.book.Location;


                }
                else
                {
                    MessageBox.Show("Выберите запись для редактирования");
                }
            


            //if (bookView.CurrentRow != null)
            //{
            //    BookViewForm fm = new BookViewForm(bookView.CurrentRow.Cells[0].Value.ToString());
            //    fm.ShowDialog();
            //}
            //else
            //{
            //    MessageBox.Show("Выберите запись для просмотра");
            //}



          
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (bookView.CurrentRow != null)
            {

                int elementId = Convert.ToInt32(bookView.CurrentRow.Cells[0].Value);
                if (books.Where(x => x.bookid == elementId).Count() == 1)
                {
                    Book element = books.Where(x => x.bookid == elementId).FirstOrDefault();
                    bool allowToDelete = true; ;
                    foreach (var x in element.InstanceBook)
                    {
                        if (x.Reader != null)
                        {
                            MessageBox.Show("Книга выдана читателю");
                            allowToDelete = false;
                            break;
                        }
                    }
                    if (allowToDelete)
                    {
                        element.BookAuthor.Clear();
                        ins.SaveChanges();

                        element.InstanceBook.Clear();
                        ins.SaveChanges();

                        books.DeleteObject(element);
                        ins.SaveChanges();
                        bookView.Rows.Remove(bookView.CurrentRow);
                    }
                }
                else
                    bookView.Rows.Remove(bookView.CurrentRow);

            }
            else
            {
                MessageBox.Show("Выберите строку для удаления");
            }
        }
    }
}
